package com.aruba.recyclerviewapplication;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aruba.recyclerviewapplication.searchfilter.LocalGroupSearch;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ItemTouchHelperActivity extends AppCompatActivity implements IMoveAndSwipeCallback {
    RecyclerView rv;
    private List<String> allStrings;
    private List<String> strings;
    SearchView searchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_touch_helper);

        searchView = findViewById(R.id.searchview);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            //文字输入完成，提交的回调
            @Override
            public boolean onQueryTextSubmit(String s) {
                ArrayList<String> resultList = LocalGroupSearch.searchGroup(s, (ArrayList<String>) allStrings);
                strings.clear();
                strings.addAll(resultList);
                rv.getAdapter().notifyDataSetChanged();
                return false;
            }

            //输入文字发生改变
            @Override
            public boolean onQueryTextChange(String s) {
                return false;
            }
        });

        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        rv.addItemDecoration(new MyDecoration(this, MyDecoration.HORIZONTAL_LIST));

        initData();
        MyAdapter myAdapter = new MyAdapter(this);
        rv.setAdapter(myAdapter);

        //给RecyclerView设置ItemTouchHelper
        ItemTouchHelperCallback itemTouchHelperCallback = new ItemTouchHelperCallback();
        itemTouchHelperCallback.setiMoveAndSwipeCallback(this);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHelperCallback);
        itemTouchHelper.attachToRecyclerView(rv);
    }

    private void initData() {
        strings = new ArrayList<>();
        allStrings = new ArrayList<>();
        String s = "南京、无锡、徐州、常州、苏州、南通、连云港、淮安、盐城bai、扬州、镇江、泰州、宿迁";
        String[] sp = s.split("、");
        for (int i = 0; i < sp.length; i++) {
            strings.add(sp[i]);
            allStrings.add(sp[i]);
        }
    }

    @Override
    public void onMove(int prePosition, int postPosition) {
        Collections.swap(strings, prePosition, postPosition);
        if (rv.getAdapter() != null) {
            rv.getAdapter().notifyItemMoved(prePosition, postPosition);
        }
    }

    @Override
    public void onSwiped(int position) {
        strings.remove(position);
        if (rv.getAdapter() != null) {
            rv.getAdapter().notifyItemRemoved(position);
        }
    }

    class MyAdapter extends RecyclerView.Adapter {
        private Context context;

        public MyAdapter(Context context) {
            this.context = context;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_text, viewGroup, false));
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
            MyViewHolder myViewHolder = (MyViewHolder) viewHolder;
            myViewHolder.tv.setText(strings.get(i));
        }

        @Override
        public int getItemCount() {
            return strings.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            public TextView tv;

            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                tv = itemView.findViewById(R.id.tv);
            }
        }
    }
}
